########################################
#Creates Figures Oc1/OC2
########################################

##########################
#Packages
#########################

#text analysis
library(tidytext)
library(quanteda)
library(SnowballC)
#data manipulation and plotting
library(rio)
library(tidyverse)

#import data
lucid <- import("./Data/lucid_coded_all.rda")


####################################
#Which Politicians
#####################################

dpoliticians <- lucid %>%
  filter(d_politicians == 1) %>%
  select(d_politicians_which) %>%
  separate(d_politicians_which, into=c("p1", "p2", "p3", "p4", "p5"),
           sep="\\|") %>%
  pivot_longer(cols=p1:p5, 
               names_to = "polnumb", 
               values_to = "politician") %>%
  filter(!is.na(politician)) %>%
  distinct(politician)

#Subset
dems1 <- lucid %>%
  filter(dem_extreme_1 == 1)

#Function
d_function1 <- function(x) {
  ifelse(grepl(x, dems1$d_politicians_which), 1, 0)
}

#Politicians
dems1$trump <- d_function1("Trump")
dems1$pelosi <- d_function1("Pelosi")
dems1$kavanaugh <- d_function1("Kavanaugh")
dems1$schumer <- d_function1("Schumer")
dems1$clinton <- d_function1("Clinton")
dems1$sanders <- d_function1("Sanders")
dems1$jfk <- d_function1("JFK")
dems1$truman <- d_function1("Truman")
dems1$barr <- d_function1("Barr")
dems1$dem_senators <- d_function1("Democratic Senators")
dems1$squad <- ifelse(grepl("AOC", dems1$d_politicians_which), 1, 
                      ifelse(grepl("Omar", dems1$d_politicians_which), 1, 
                             ifelse(grepl("Radical Members", dems1$d_politicians_which), 1, 
                                    ifelse(grepl("Democratic House Members", dems1$d_politicians_which),1, 
                                           ifelse(grepl("New House Democrats", dems1$d_politicians_which), 1, 0)))))

###Count
d_politician_count <- dems1 %>%
  select(trump, pelosi, kavanaugh, schumer, clinton, sanders, jfk, truman, 
         barr, dem_senators, squad) %>%
  pivot_longer(
    cols= trump:squad,
    names_to = "category", 
    values_to = "responses") %>%
  count(responses, category, sort=T) %>%
  filter(responses==1) %>%
  mutate(overall_pct = n/511, 
         issue_pct = n/67) %>%
  mutate(category = recode(category, trump = "Trump", 
                           pelosi = "Pelosi", 
                           kavanaugh = "Kavanaugh", 
                           schumer = "Schumer", 
                           sanders = "Sanders", 
                           clinton = "H. Clinton", 
                           jfk = "JFK", 
                           truman = "Truman", 
                           barr = "B. Barr", 
                           dem_senators = "Dem Senators", 
                           squad = "The Squad"))

###Republicans
rpoliticians <- lucid %>%
  filter(r_politicians == 1) %>%
  select(r_politicians_which) %>%
  separate(r_politicians_which, into=c("p1", "p2", "p3", "p4", "p5", "p6", "p7"),
           sep="\\|") %>%
  pivot_longer(cols=p1:p7, 
               names_to = "polnumb", 
               values_to = "politician") %>%
  filter(!is.na(politician)) %>%
  distinct(politician)

#Subset
reps1 <- lucid %>%
  filter(rep_extreme_1 == 1)

#Function
r_function1 <- function(x) {
  ifelse(grepl(x, reps1$r_politicians_which), 1, 0)
}

##Politicians
reps1$trump <- r_function1("Trump")
reps1$pelosi <- r_function1("Pelosi")
reps1$mcconnell <- r_function1("McConnell")
reps1$wbush <- r_function1("W.Bush")
reps1$kavanaugh <- r_function1("Kavanaugh")
reps1$putin <- r_function1("Putin")
reps1$s_sanders <- r_function1("S.Sanders")
reps1$pence <- r_function1("Pence")
reps1$gingrich <- r_function1("Gingrich")
reps1$b_clinton <- r_function1("B. Clinton")
reps1$lewinsky <- r_function1("Lewinsky")
reps1$obama <- r_function1("Obama")
reps1$mccain <- r_function1("McCain")

##Count
r_politician_count <- reps1 %>%
  select(trump, pelosi, mcconnell, wbush, kavanaugh, putin, s_sanders, pence, 
         gingrich, b_clinton, lewinsky, obama, mccain) %>%
  pivot_longer(
    cols= trump:mccain, 
    names_to = "category", 
    values_to = "responses") %>%
  count(responses, category, sort=T) %>%
  filter(responses==1) %>%
  mutate(overall_pct = n/532, 
         issue_pct = n/92) %>%
  mutate(category = recode(category, trump = "Trump", 
                           pelosi = "Pelosi", 
                           kavanaugh = "Kavanaugh", 
                           mcconnell = "McConnell", 
                           wbush = "W. Bush", 
                           putin = "Putin", 
                           s_sanders = "S. Sanders", 
                           pence = "Pence", 
                           gingrich = "Gingrich", 
                           b_clinton = "B. Clinton", 
                           lewinsky = "Lewinsky", 
                           obama = "Obama", 
                           mccain = "McCain"))


##plot 1
dpol_plot1 <- ggdotchart(d_politician_count, x = "category", y="n", 
                         sorting = "descending", 
                         add = "segments", 
                         dot.size=9, label=round(d_politician_count$n, 2), 
                         font.label = list(color="white", size=8, vjust=0.5),
                         ggtheme=theme_pubr()) + 
  labs(title = "Democratic Party", x = "Category", y = "Count")

rpol_plot1 <- ggdotchart(r_politician_count, x = "category", y="n", 
                         sorting = "descending", 
                         add = "segments", 
                         dot.size=9, label=round(r_politician_count$n, 2), 
                         font.label = list(color="white", size=8, vjust=0.5),
                         ggtheme=theme_pubr()) + 
  labs(title = "Republican Party", x = "Category", y = "Count")

plot_grid(dpol_plot1, rpol_plot1, labels=c("A", "B"))


ggsave("./figure_oc2.png", height=8, width=12)


####################################
#Which Issues
#####################################

#Getting a list of mentioned policies
dpolicies <- lucid %>%
  filter(d_policy == 1) %>%
  select(d_policy_which) %>%
  separate(d_policy_which, into=c("p1", "p2", "p3", "p4", "p5"),
           sep="\\|") %>%
  pivot_longer(cols=p1:p5, 
               names_to = "issnumb", 
               values_to = "issue") %>%
  filter(!is.na(issue)) %>%
  distinct(issue)

#Subset
dems <- lucid %>%
  filter(dem_extreme_1 == 1)

#Function
d_function <- function(x) {
  ifelse(grepl(x, dems$d_policy_which), 1, 0)
}

#Policies we can apply the function to 
dems$d_guns <- d_function("Gun Control")
dems$d_taxes <- d_function("Taxes")
dems$d_ss <- d_function("Social Security")
dems$d_cp <- d_function("Capital Punishment")
dems$d_tuition <- d_function("College Tuition")
dems$d_spending <- d_function("Spending")
dems$d_deficit <- d_function("Deficit")
dems$d_drugs <- d_function("Drugs")
dems$d_reparations <- d_function("Reparations")
dems$d_anthem <- d_function("National Anthem")
dems$d_war <- d_function("War")
dems$d_entitlements <- d_function("Entitlements")
dems$d_economy <- d_function("Economy")
dems$d_climate <- d_function("Climate Change")

dems$d_abortion <- d_function("Abortion")
dems$d_birth <- d_function("Birth Control")

#We will combine abortion and birth control together
dems$d_reproduction <- ifelse(grepl("Abortion", dems$d_policy_which), 1, 
                              ifelse(grepl("Birth Control", dems$d_policy_which), 1, 0))

dems$d_immigration <- ifelse(grepl("Immigration", dems$d_policy_which), 1, 
                             ifelse(grepl("Immimgration", dems$d_policy_which), 1, 0))

dems$d_healthcare <- ifelse(grepl("Medicare for All", dems$d_policy_which), 1, 
                            ifelse(grepl("Health Care", dems$d_policy_which), 1, 
                                   ifelse(grepl("Medicare For All", dems$d_policy_which), 1, 
                                          ifelse(grepl("Medicare", dems$d_policy_which), 1, 0))))

dems$d_voting <- ifelse(grepl("Electoral College", dems$d_policy_which), 1, ifelse(grepl("Voting Regulations", 
                                                                                         dems$d_policy_which), 1, 0))

##Get the counts
d_policy_count <- dems %>%
  select(d_guns, d_taxes, d_ss, d_cp, d_tuition, d_spending, d_deficit, d_drugs, d_reparations,
         d_anthem, d_war, d_entitlements, d_economy, d_climate, d_reproduction, d_immigration,
         d_healthcare, d_voting) %>%
  pivot_longer(
    cols=starts_with("d_"),
    names_to = "category", 
    values_to = "responses") %>%
  count(responses, category, sort=T) %>%
  filter(responses==1) %>%
  mutate(overall_pct = n/511, 
         issue_pct = n/93) %>%
  mutate(category = recode(category, d_immigration = "Immigration", 
                           d_anthem = "Nat. Anthem", 
                           d_climate = "Environment", 
                           d_cp = "Capital Punishment", 
                           d_deficit = "Deficit", 
                           d_drugs = "Drugs", 
                           d_healthcare = "Health Care",
                           d_economy = "Economy", 
                           d_entitlements = "Entitlements", 
                           d_guns = "Gun Control", 
                           d_reparations = "Reparations", 
                           d_reproduction = "Abortion/Birth Control", 
                           d_spending = "Spending", 
                           d_ss = "Social Security", 
                           d_taxes = "Taxes", 
                           d_tuition = "College Tuition", 
                           d_voting = "Voting Policy", 
                           d_war = "War"))

#Plot 1: Count
library(ggpubr)

dplot1 <- ggdotchart(d_policy_count, x = "category", y="n", 
                     sorting = "descending", 
                     add = "segments", 
                     dot.size=9, label=round(d_policy_count$n, 2), 
                     font.label = list(color="white", size=8, vjust=0.5),
                     ggtheme=theme_pubr()) + 
  labs(title = "Democratic Party", x = "Category", y = "Count")


#Plot 2: Pct of those coded as using an issue

dplot2 <- ggdotchart(d_policy_count, x = "category", y="issue_pct", 
                     sorting = "descending", 
                     add = "segments", 
                     dot.size=9, label=round(d_policy_count$issue_pct, 2), 
                     font.label = list(color="white", size=8, vjust=0.5),
                     ggtheme=theme_pubr()) + 
  labs(title = "Democratic Party", x = "Category", y = "Proportion of Issue Codes")


###Republican Policies
#Getting a list of mentioned policies
rpolicies <- lucid %>%
  filter(r_policy == 1) %>%
  select(r_policy_which) %>%
  separate(r_policy_which, into=c("p1", "p2", "p3", "p4", "p5", 
                                  "p6", "p7", "p8"),
           sep="\\|") %>%
  pivot_longer(cols=p1:p8, 
               names_to = "issnumb", 
               values_to = "issue") %>%
  filter(!is.na(issue)) %>%
  distinct(issue)

##Function
r_function <- function(x) {
  ifelse(grepl(x, reps$r_policy_which), 1, 0)
}

##Subset
reps <- lucid %>%
  filter(rep_extreme_1 == 1)

###Policies
reps$r_ss <- r_function("Social Security")
reps$r_space <- r_function("Space")
reps$r_religion <- r_function("Religion")
reps$r_economy <- r_function("Economy")
reps$r_drugs <- r_function("Marijuana")
reps$r_cp <- r_function("Death Penalty")
reps$r_guns <- r_function("Gun Control")
reps$r_education <- r_function("Education")
reps$r_fp <- r_function("Foreign Policy")
reps$r_infrastructure <- r_function("Infrastructure")
reps$r_spending <- r_function("Spending")
reps$r_taxes <- r_function("Taxes")
reps$r_unemployment <- r_function("Unemployment")

reps$r_deregulation <- ifelse(grepl("Deregulation", reps$r_policy_which), 1, 
                              ifelse(grepl("Regulation", reps$r_policy_which), 1, 0))

reps$r_deficit <- ifelse(grepl("Deficit", reps$r_policy_which), 1, 
                         ifelse(grepl("Deficits", reps$r_policy_which),1,0))

reps$r_immigration <- ifelse(grepl("Immigration", reps$r_policy_which), 1, 
                             ifelse(grepl("immigration", reps$r_policy_which), 1, 0))

reps$r_environment <- ifelse(grepl("Coal", reps$r_policy_which), 1, 
                             ifelse(grepl("Climate Change", reps$r_policy_which), 1, 
                                    ifelse(grepl("Drilling", reps$r_policy_which), 1, 
                                           ifelse(grepl("Environment", reps$r_policy_which), 1, 0))))

reps$r_healthcare <- ifelse(grepl("Health Care", reps$r_policy_which), 1, 
                            ifelse(grepl("Health  Care", reps$r_policy_which), 1, 
                                   ifelse(grepl("Medicare", reps$r_policy_which),1,0)))

reps$r_reproduction <- ifelse(grepl("Abortion", reps$r_policy_which), 1, 
                              ifelse(grepl("Birth Control", reps$r_policy_which), 1, 0))

reps$r_voting <- ifelse(grepl("Voting", reps$r_policy_which ), 1, 
                        ifelse(grepl("Citizens United", reps$r_policy_which), 1, 0))

reps$r_women <- ifelse(grepl("Women's Issues", reps$r_policy_which), 1, 
                       ifelse(grepl("ERA",reps$r_policy_which ), 1, 
                              ifelse(grepl("Violence Against Women Act", reps$r_policy_which), 1, 0)))

reps$r_homosexuality <- ifelse(grepl("Gay Marriage", reps$r_policy_which), 1, 
                               ifelse(grepl("Gay Rights", reps$r_policy_which ), 1, 
                                      ifelse(grepl("Homosexuality", reps$r_policy_which), 1, 0)))

##Count
r_policy_count <- reps %>%
  select(r_ss, r_space, r_deficit, r_religion, r_immigration, r_economy, r_drugs, r_cp, 
         r_guns, r_environment, r_healthcare, r_reproduction, r_education, r_fp,  
         r_deregulation, r_voting, r_women, r_homosexuality, r_infrastructure, r_spending, r_taxes, 
         r_unemployment) %>%
  pivot_longer(
    cols=starts_with("r_"),
    names_to = "category", 
    values_to = "responses") %>%
  count(responses, category, sort=T) %>%
  filter(responses==1) %>%
  mutate(overall_pct = n/532, 
         issue_pct = n/105) %>%
  mutate(category = recode(category, 
                           r_unemployment  = "Unemployment", 
                           r_taxes = "Taxes", 
                           r_spending = "Spending", 
                           r_infrastructure = "Infrastructure", 
                           r_homosexuality = "LGTGQ Policies", 
                           r_women = "Gender Policy", 
                           r_voting = "Voting Policy", 
                           r_deregulation = "Deregulation", 
                           r_deficit = "Deficit", 
                           r_fp = "Foreign Policy", 
                           r_education = "Education", 
                           r_reproduction = "Abortion/Birth Control", 
                           r_healthcare = "Health Care", 
                           r_environment = "Environment", 
                           r_guns = "Gun Control", 
                           r_cp = "Capital Punishment", 
                           r_drugs = "Drugs", 
                           r_economy = "Economy", 
                           r_immigration = "Immigration", 
                           r_religion = "Religion", 
                           r_space = "Space", 
                           r_ss = "Social Security"))

#Count
rplot1 <- ggdotchart(r_policy_count, x = "category", y="n", 
                     sorting = "descending", 
                     add = "segments", 
                     dot.size=9, label=round(r_policy_count$n, 2), 
                     font.label = list(color="white", size=8, vjust=0.5),
                     ggtheme=theme_pubr()) + 
  labs(title = "Republican Party", x = "Category", y = "Count")

#Issue Pct
rplot2 <- ggdotchart(r_policy_count, x = "category", y="issue_pct", 
                     sorting = "descending", 
                     add = "segments", 
                     dot.size=9, label=round(r_policy_count$issue_pct, 2), 
                     font.label = list(color="white", size=8, vjust=0.5),
                     ggtheme=theme_pubr()) + 
  labs(title = "Republican Party", x = "Category", y = "Proportion of Issue Codes")


plot_grid(dplot1, rplot1, 
          labels=c("A", "B"))


ggsave("figure_oc1.png", height=8, width=12)
